package com.jintoufs.weixin.coupon;

import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.coupon.Coupon;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.customer.CustomerCoupon;
import com.jintoufs.service.coupon.CouponService;
import com.jintoufs.service.customer.CustomerCouponService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.weixin.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/coupon")
public class CouponAction extends SupportAction{
    @Resource
    private CouponService couponService;

    @Resource
    private CustomerCouponService customerCouponService;

    @GetMapping("/list")
    public AjaxMsg list(String type,String token){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Map<String,Object> param = new HashMap<>();
        Customer customer = getCustomerByToken(token);
        try {
            if (customer!=null){
                param.put("customerId",customer.getId());
                param.put("used","used");
                List<String> ids = customerCouponService.queryIdByParam(param);
                if (type==null){
                    type="";
                }
                param.clear();
                switch (type){
                    case Constants.COUPON_TYPE_HOTEL:
                        param.put("typeId",Constants.COUPON_TYPE_HOTEL_ID);
                        break;
                    case Constants.COUPON_TYPE_SCENE:
                        param.put("typeId",Constants.COUPON_TYPE_SCENE_ID);
                        break;
                    case Constants.COUPON_TYPE_TOUR:
                        param.put("typeId",Constants.COUPON_TYPE_TOUR_ID);
                        break;
                    default:
                        break;
                }
                param.put("ids", ValidUtil.isEmpty(ids) ? "" : ids);
                param.put("noMy","noMy");
                ajaxMsg.setDatas(couponService.selectByIds(param));
            }else {
                ajaxMsg.setCode(AjaxMsg.FAILURE);
                ajaxMsg.setMessage("身份验证失败，请重新登陆");
            }
        }catch (Exception e){
            e.printStackTrace();
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("服务器异常");
        }
        return ajaxMsg;
    }

    @GetMapping("/receive")
    public AjaxMsg receive(String token,Integer id){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (!ValidUtil.isEmpty(customer)){
            Map<String,Object> param = new HashMap<>();
            param.put("couponId",id);
            param.put("customerId",customer.getId());
            if (customerCouponService.queryByParam(param).size()==0){
                Coupon coupon = couponService.getById(id);
                Date now = new Date();
                if (coupon.getSendStartDate().before(now)&&coupon.getSendEndDate().after(now)){
                    CustomerCoupon customerCoupon = new CustomerCoupon();
                    customerCoupon.setCouponId(id);
                    customerCoupon.setCustomerId(customer.getId());
                    customerCoupon.setCreateTime(now);
                    customerCoupon.setId(getUUID());
                    customerCouponService.inset(customerCoupon);
                }else {
                    ajaxMsg.setCode(AjaxMsg.FAILURE);
                    ajaxMsg.setMessage("优惠券已不能领取");
                }
            }else {
                ajaxMsg.setCode(AjaxMsg.FAILURE);
                ajaxMsg.setMessage("用户已领取");
            }
        }else {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("身份验证失败，请重新登陆");
        }
        return ajaxMsg;
    }

    @GetMapping("/myCoupon")
    public AjaxMsg myCoupon(String token){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (customer!=null){
            Map<String,Object> param = new HashMap<>();
            param.put("customerId",customer.getId());
            param.put("used","used");
            List<String> ids = customerCouponService.queryIdByParam(param);
            param.clear();
            param.put("ids", ValidUtil.isEmpty(ids)?"":ids);
            param.put("my","my");
            ajaxMsg.setDatas(couponService.selectByIds(param));
        }else {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("身份验证失败，请重新登陆");
        }
        return ajaxMsg;
    }
}
